package com.tiles.interceptor;

import com.tiles.service.TilesUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class tilesInterceptor   implements HandlerInterceptor {
    @Autowired
    private TilesUserService service;

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {


        System.out.println("============拦截器处理==================>");

        Cookie[] cookies = request.getCookies();

        if (request.getRequestURI().contains("/u")){
            Jedis jedis = new Jedis();
            for (Cookie cookie : cookies) {
                if (jedis.get(cookie.getValue())!=null){
                    return true;
                }
            }
            jedis.close();
           request.setAttribute("msg","需要登陆才能访问");
            request.getRequestDispatcher("/WEB-INF/view/testTiles/login.jsp").forward(request,response);
            return false;
        }
        return true;
    }


}
